package com.ruoyi.sensitive;


import toolgood.words.IllegalWordsSearch;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class SensitiveWordsUtilV1 {
    private List<String> allWoeds = new ArrayList<>();

    private IllegalWordsSearch allWords = new IllegalWordsSearch();

    private volatile static SensitiveWordsUtilV1 instance ;
    private SensitiveWordsUtilV1(){
        File adFile = new File(this.getResourcesFilePath("sensitive_words.txt"));

        allWords.SetKeywords(loadKeywords(adFile));

    }

    public static SensitiveWordsUtilV1 getInstance(){
        if (instance == null) {
            synchronized (SensitiveWordsUtilV1.class) {
                if (instance == null) {
                    instance = new SensitiveWordsUtilV1();
                }
            }
        }
        return instance;
    }

    private static List<String> loadKeywords(File file) {
        List<String> keyArray = new ArrayList<String>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {// 使用readLine方法，一次读一行
                keyArray.add(s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return keyArray;
    }

    private String getResourcesFilePath(String fileName){
        return this.getClass().getClassLoader().getResource(fileName).getPath();
    }
    public IllegalWordsSearch getAdWords() {
        return allWords;
    }
    public List<String> getAdListWords() {
        return allWoeds;
    }
}
